<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : !disabled ? '修改' : '查看'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form inline :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="100px">
      <el-form-item label="所属店铺" prop="shopsName">
        <el-input v-model="dataForm.shopsName" :disabled="disabled" placeholder="所属店铺"></el-input>
      </el-form-item>
      <el-form-item label="商品名称" prop="goodsName">
        <el-input v-model="dataForm.goodsName" :disabled="disabled" placeholder="商品名称"></el-input>
      </el-form-item>
      <el-form-item label="商品编码" prop="goodsSn">
        <el-input v-model="dataForm.goodsSn" :disabled="disabled" placeholder="商品编码"></el-input>
      </el-form-item>
      <el-form-item label="规格属性" prop="goodsSpecifitionNameValue">
        <el-input v-model="dataForm.goodsSpecifitionNameValue" :disabled="disabled"
                  placeholder="规格属性组成的字符串，用来显示用"></el-input>
      </el-form-item>
      <el-form-item label="市场价" prop="marketPrice">
        <el-input v-model="dataForm.marketPrice" :disabled="disabled" placeholder="市场价"></el-input>
      </el-form-item>
      <el-form-item label="零售价格" prop="retailPrice">
        <el-input v-model="dataForm.retailPrice" :disabled="disabled" placeholder="零售价格"></el-input>
      </el-form-item>
      <el-form-item label="数量" prop="number">
        <el-input v-model="dataForm.number" :disabled="disabled" placeholder="数量"></el-input>
      </el-form-item>
      <el-form-item label="商品图片" prop="listPicUrl">
        <el-img v-model="dataForm.listPicUrl" :disabled="disabled">
        </el-img>
      </el-form-item>
      <el-form-item label="是否选中" prop="checked">
        <el-radio-group v-model="dataForm.checked" :disabled="disabled">
          <el-radio :label="0">否</el-radio>
          <el-radio :label="1">是</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button v-if="!disabled" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        disabled: false,
        visible: false,
        dataForm: {
          id: '',
          userId: '',
          goodsId: '',
          goodsSn: '',
          goodsName: '',
          skuId: '',
          marketPrice: '',
          retailPrice: '',
          number: '',
          goodsSpecifitionNameValue: '',
          checked: '',
          listPicUrl: '',
          shopsId: '',
          shopsName: ''
        },
        dataRule: {
          name: [
            {
              required: true,
              message: '名称不能为空',
              trigger: 'blur'
            }
          ]
        }
      }
    },
    methods: {
      init (id, shopsName, disabled) {
        this.disabled = disabled
        this.dataForm.id = id || ''
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: `/mall/cart/info/${this.dataForm.id}`,
              method: 'get'
            }).then(({ data }) => {
              if (data && data.code === 0) {
                this.dataForm = data.cart
                this.dataForm.shopsName = shopsName || ''
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm']
          .validate((valid) => {
            if (valid) {
              this.$http({
                url: `/mall/cart/${!this.dataForm.id ? 'save' : 'update'}`,
                method: 'post',
                data: this.dataForm
              }).then(({ data }) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500
                  })
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            }
          })
      }
    }
  }
</script>
